package operation;

import book.BookList;
import book.Book;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @ClassDescription:
 * @JdkVersion: 1.8
 * @Author: 李白
 * @Created: 2024/7/29 21:58
 */
public class DelOperation implements IOPeration{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除书籍");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的图书名字");
        String name = scanner.nextLine();
        int currentSize = bookList.getUsedSize();
        int pos = -1;
        //寻找要删除的书
        int i = 0;
        for (; i < currentSize ; i++) {
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)){
                pos = i;
                break;
            }
        }
        //没有找到的情况
        if (i == currentSize ){
            System.out.println("查无此书");
        }
        //找到了然后变化位置
        int j = pos;
        for (; j < currentSize - 1; j++) {
            Book book = bookList.getBook(j+1);
            bookList.setBooks(j,book);
        }
        bookList.setBooks(j,null);
        bookList.setUsedSize(currentSize-1);
        System.out.println("删除成功");
    }
}
